/**
 * Copyright 2020 Inc.
 **/
package com.myz.cloud.rocketmq.consumer.mq;


import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;

import java.nio.charset.StandardCharsets;

/**
 * DefaultMessageListenerConcurrently
 *
 * @author maoyz0621 on 2020/8/11
 * @version v1.0
 */
@Slf4j
public abstract class BaseRocketMQListener<T> implements RocketMQListener<MessageExt> {

    @Autowired
    protected ObjectMapper objectMapper;

    @Override
    public void onMessage(MessageExt message) {
        String msgId = message.getMsgId();
        byte[] body = message.getBody();
        int reconsumeTimes = message.getReconsumeTimes();
        int queueId = message.getQueueId();
        String topic = message.getTopic();
        String tags = message.getTags();
        log.info("msgId={},topic={},tags = {}, queueId={}, reconsumeTimes={}", msgId, topic, tags, queueId, reconsumeTimes);
        // 设置消费次数上限
        if (reconsumeTimes > 3) {
            return;
        }
        doConsumerMsg(new String(body, StandardCharsets.UTF_8));
    }

    /**
     * 执行消费
     *
     * @param message
     */
    protected abstract void doConsumerMsg(String message);

}